package top.gohigh.commonview;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.content.ContextCompat;

public class GHCountDownBtn extends AppCompatButton {
    CountDownTimer timer;
    String defStr;
    int remainSecond=0;
    public GHCountDownBtn(Context context) {
        super(context);
    }

    public GHCountDownBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundResource(R.drawable.primary_button);
    }

    public GHCountDownBtn(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundResource(R.drawable.primary_button);
    }


    /**
     * 禁用s秒
     * @param timeSecond
     */
    public void disable(int timeSecond){
        defStr=getText().toString();
        remainSecond=timeSecond;
        setEnabled(false);
        if(timer!=null)timer.cancel();
        timer=new CountDownTimer(timeSecond*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                setText(remainSecond--+"s");
            }

            @Override
            public void onFinish() {
                setText(defStr);
                setEnabled(true);
            }
        };
        timer.start();
    }

    public void reset(){
        if(timer!=null){
            timer.cancel();
        }
        setText(defStr);
        setEnabled(true);
    }


}
